// pages/short-video-list/video-list.js
import {
  liveStatus
} from '../../utils/const.js'

// 直播状态
const liveBtn = {
  '101': '观看直播',
  '102': '查看预告',
  '103': '查看回放'
}

const iconMap = {
  '101': '../../assets/images/icon/playing.png',
  '102': '../../assets/images/icon/no-play.png',
  '103': '../../assets/images/icon/play-back.png'
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 直播列表
    list: [],
    // 回放列表
    pageList: [],
    page: 1,
    pageSize: 10,
    totalPages: 0
  },
  itemClickHandle(event) {
    const {
      index
    } = event.currentTarget.dataset
    let {
      roomid
    } = this.data.list[index]
    wx.navigateTo({
      url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomid}`
    })
  },
  backClickHandle(event) {
    const {page, index} = event.currentTarget.dataset
    let {roomid} = this.data.pageList[page][index]
    wx.navigateTo({
      url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomid}`
    })
  },
  requestList() {
    wx.ajax
      .post('queryDoingLiveList.action', {merchant_id: this.merchant_id})
      .then(({
        time,
        result
      }) => {
        let date = new Date(time.replaceAll('-', '/'))

        result.forEach(item => {
          item.statusName = liveStatus[item.live_status]
          item.btnText = liveBtn[item.live_status]
          item.iconUrl = iconMap[item.live_status]
          if (item.live_status == '102') {
            item.countDown = this.computedTime((item.start_time * 1000) - date.getTime())
          }
        })

        wx.stopPullDownRefresh()
        this.setData({
          list: result
        });
      })
      .catch(() => {
        wx.stopPullDownRefresh()
      });
  },
  computedTime(timeDuration) {
    let timeArr = [0, 0, 0]
    if (timeDuration > 0) {
      let miniteNum = 60 * 1000,
        hourNum = 60 * miniteNum,
        dayNum = 24 * hourNum;
      let temp = Math.floor(timeDuration / dayNum),
        str = '';
      if (temp > 0) {
        timeArr[0] = temp
      }
      timeDuration = timeDuration % dayNum;
      temp = Math.floor(timeDuration / hourNum)
      if (temp > 0) {
        timeArr[1] = temp
      }
      timeDuration = timeDuration % hourNum;
      temp = Math.floor(timeDuration / miniteNum)
      if (temp > 0) {
        timeArr[2] = temp
      }
    }
    return `${timeArr[0]}天${timeArr[1]}时${timeArr[2]}分`
  },
  // 请求回放列表
  requestBackList(page) {
    if (page > 1 && page > this.data.totalPages) {
      return
    }

    wx.ajax
      .post('queryReplayList.action', {
        merchant_id: this.merchant_id,
        page,
        pageSize: this.data.pageSize
      })
      .then(({
        result
      }) => {
        let data = result || {};
        data.pageList = data.pageList || []
        data.pageList.forEach(item => {
          item.statusName = liveStatus[item.live_status]
          item.btnText = liveBtn[item.live_status]
          item.iconUrl = iconMap[item.live_status]
        })

        this.setData({
          page: data.page,
          totalPages: data.totalPages,
          [`pageList[${data.page - 1}]`]: data.pageList
        });
      })
  },
  requestRefresh() {
    this.requestList()
    this.requestBackList()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.startPullDownRefresh()
    this.merchant_id = options.merchant_id
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.requestRefresh()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.requestRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.requestBackList(this.data.page + 1)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: `/pages/live-center/live-center`
    }
  }
})